<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>

{% csrf_token %}

<h1>jquery_ajax</h1>
<script>
    var csrf_token = $("[name='csrfmiddlewaretoken']").val()  //.val) 获取csrf_token的属性 value
    var test = "123"  //定义数据值
    var data = {"test": test, 'csrfmiddlewaretoken': csrf_token}
    $.ajax({
        type: "POST",   //以POST方式传送
        url: "/ajax_api/",  //传送的url路径
        data: data,  //传送数据
        success: function (result) {
            if( result.code == '0') {
                console.log(result.msg);
            } else if (result.code == '1') {
                alert (result.msg)
            }
        },
        error: function () {
            alert("请求服务器接口失败！")
        }
    })
</script>
</body>
</html